package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.bean.DotBean;
import com.db.JdbcUtil;
import com.exception.BorrowDotException;
import com.exception.DBException;

public class BorrowDotDao
{
	private Connection con = null;
	private ResultSet rs = null;
	private PreparedStatement ps = null;

	public void updateProvisional(DotBean pro) throws DBException,
			BorrowDotException
	{
		String sql = "Update lab_provisional set context=? where userID=?";
		con = JdbcUtil.getConnection();
		try
		{
			ps = con.prepareStatement(sql);
			ps.setString(1, pro.getContext());
			ps.setString(2, pro.getUserID());
			ps.executeUpdate();
		}
		catch (Exception e)
		{
			throw new BorrowDotException("Fail to change the content!");
		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
	}

	public void getProvisional(DotBean pro) throws DBException,
			BorrowDotException
	{
		String sql = "select a.userID,a.firstname,a.lastname,a.gender,a.email,c.context from lab_user a join lab_provisional c on c.userID=a.userID where a.userID=?";
		con = JdbcUtil.getConnection();
		try
		{
			ps = con.prepareStatement(sql);
			ps.setString(1, pro.getUserID());
			rs = ps.executeQuery();

			while (rs.next())
			{
				pro.setUserID(rs.getString(1));
				pro.setFirstname(rs.getString(2));
				pro.setLastname(rs.getString(3));
				pro.setGender(rs.getString(4));
				pro.setEmail(rs.getString(5));
				pro.setContext(rs.getString(6));
			}

		}
		catch (Exception e)
		{
			e.printStackTrace();
			throw new BorrowDotException("Fail to get user borrow dot info");
		}
		finally
		{
			JdbcUtil.free(rs, ps, con);
		}
	}
}
